/*###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############*/

var IUsers_Places = {
    getCitiesForCountry : function(rootUrl, countryObj, cityId){
        var cityObj = document.getElementById(cityId);
        this.countryObj = countryObj;
        this.cityObj = cityObj;
        if(!document.getElementById('IUsers_LoadingImage')){
            var loading = document.createElement('div');
            loading.style.display = 'inline';
            loading.id = 'IUsers_LoadingImage'; 
            cityObj.parentNode.appendChild(loading);
        };
        var img = document.createElement('img');
        img.src = rootUrl+'/modules/IUsers/images/loading_trans.gif';
        img.alt = 'Loading ...';
        img.style.marginLeft = '8px';
        document.getElementById('IUsers_LoadingImage').innerHTML = '';
        document.getElementById('IUsers_LoadingImage').appendChild(img);
        cityObj.disabled = true;
        cityObj.innerHTML = '';
        cityObj[cityObj.length] = new Option('Loading ...', '0');
        var countryId = AntzBase.dom_dropMenuSelectionKey(countryObj);
        this.ajax = AntzBase.createAjax();
        this.ajax.open('POST', rootUrl+'/ajax_cities_for_country/?country_id='+countryId);
        this.ajax.onreadystatechange = IUsers_Places.parseGetCitiesForCountry;
        this.ajax.send(null);
    },
    parseGetCitiesForCountry : function(){
        var ajax = IUsers_Places.ajax;
        if(ajax.readyState == 4){
            if(ajax.status == 200){
                var json = ajax.responseText;
                var cities = eval("("+json+")");
                IUsers_Places.buildCities(cities);
                document.getElementById('IUsers_LoadingImage').innerHTML = '';
            };
        };
    },
    buildCities : function(citiesArray){
        if(!this.cityObj) return;
        this.cityObj.innerHTML = '';
        for(var i=0; i<citiesArray.length; i++){
            this.cityObj[this.cityObj.length] = new Option(citiesArray[i].title, citiesArray[i].uid);
        };
        this.cityObj.disabled = false;
    }
}
